package terminal

import (
	"device-admin/config/mysql"
	"device-admin/internal/models"
	"time"
)

// 升级中
// status 1 同步中 2 同步失败 3 同步成功
func UpdateThemeStatus(robotID, terminalType, themeID string, status int) {
	db := mysql.NewDB()
	var ThemeTerminalInfo models.MaterialThemeTerminals
	var count int64
	db.Model(&models.MaterialThemeTerminals{}).
		Where("robot_id = ? and theme_id = ?", robotID, themeID).
		Where("terminal_type = ?", terminalType).
		Take(&ThemeTerminalInfo).
		Count(&count)
	if count != 0 {
		if status == 2 { //同步失败
			db.Model(&models.MaterialThemeTerminals{}).
				Where("robot_id = ? and theme_id = ?", robotID, themeID).
				Where("terminal_type = ?", terminalType).
				Updates(map[string]interface{}{
					"upgrade_status": 2, //同步失败
					"updated_at":     time.Now().Unix(),
				})
		} else if status == 1 { //同步中
			if ThemeTerminalInfo.UpgradeType == 3 { //删除
				db.Model(&models.MaterialThemeTerminals{}).
					Where("robot_id = ? and theme_id = ?", robotID, themeID).
					Where("terminal_type = ?", terminalType).
					Delete(&models.MaterialThemeTerminals{})
			} else {
				db.Model(&models.MaterialThemeTerminals{}).
					Where("robot_id = ? and theme_id = ?", robotID, themeID).
					Where("terminal_type = ?", terminalType).
					Updates(map[string]interface{}{
						"upgrade_status": 1, //同步中
						"updated_at":     time.Now().Unix(),
					})
			}
		} else if status == 3 { //同步成功
			db.Model(&models.MaterialThemeTerminals{}).
				Where("robot_id = ? and theme_id = ?", robotID, themeID).
				Where("terminal_type = ?", terminalType).
				Updates(map[string]interface{}{
					"upgrade_status": 3, //同步成功
					"updated_at":     time.Now().Unix(),
				})
		}
	}
}
